package com.cskaoyan.service.promote;

import com.cskaoyan.bean.promote.Coupon;
import com.cskaoyan.bean.promote.CouponExample;
import com.cskaoyan.bean.promote.CouponUser;
import com.cskaoyan.bean.promote.CouponUserExample;
import com.cskaoyan.bean.promote.base.PromotePageData;
import com.cskaoyan.bean.promote.base.PromoteParam;
import com.cskaoyan.bean.promote.bo.CouponListSearchBO;
import com.cskaoyan.bean.promote.bo.CouponUserListSearchBO;
import com.cskaoyan.bean.promote.enumbean.CouponTimeTypeEnum;
import com.cskaoyan.bean.promote.enumbean.CouponTypeEnum;
import com.cskaoyan.bean.promote.exception.AddCouponException;
import com.cskaoyan.bean.promote.exception.DeleteCouponException;
import com.cskaoyan.bean.promote.exception.GetCouponException;
import com.cskaoyan.bean.promote.exception.UpdateCouponException;
import com.cskaoyan.mapper.promote.CouponMapper;
import com.cskaoyan.mapper.promote.CouponUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author liXiaoFei
 * @date 2021/10/18 17:09
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public PromotePageData queryCouponList(PromoteParam promoteParam, CouponListSearchBO couponListSearchBO) {

        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria();
        if (couponListSearchBO.getName() != null && !"".equals(couponListSearchBO.getName())){
            criteria.andNameLike("%" + couponListSearchBO.getName() + "%");
        }
        if (couponListSearchBO.getType() != null){
            criteria.andTypeEqualTo(couponListSearchBO.getType());
        }
        if (couponListSearchBO.getStatus() != null){
            criteria.andStatusEqualTo(couponListSearchBO.getStatus());
        }

        example.setOrderByClause(promoteParam.getSort() + " " + promoteParam.getOrder());
        List<Coupon> coupons = couponMapper.selectByExample(example);

        return PromotePageData.ok(coupons);
    }

    @Override
    public Coupon addCoupon(Coupon coupon) throws AddCouponException {
        couponTimeType(coupon);
        if (coupon.getType() == CouponTypeEnum.CONVERSION_CODE.ordinal()){
            coupon.setCode(UUID.randomUUID().toString());
        }
        int code = couponMapper.insertSelective(coupon);
        if (code == 0) throw new AddCouponException();
        return coupon;
    }

    @Override
    public void deleteCoupon(Coupon coupon) throws DeleteCouponException {
        int code = couponMapper.updateByPrimaryKeySelective(coupon);
        if (code == 0) throw new DeleteCouponException();

    }

    @Override
    public Coupon getCouponById(Integer id) throws GetCouponException {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        if (coupon == null) throw new GetCouponException();
        return coupon;
    }

    @Override
    public PromotePageData queryCouponUserList(PromoteParam promoteParam,CouponUserListSearchBO couponUserListSearchBO) {
        CouponUserExample example = new CouponUserExample();
        CouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andCouponIdEqualTo(couponUserListSearchBO.getCouponId());

        if (couponUserListSearchBO.getUserId() != null){
            criteria.andUserIdEqualTo(couponUserListSearchBO.getUserId());
        }
        if (couponUserListSearchBO.getStatus() != null){
            criteria.andUserIdEqualTo(couponUserListSearchBO.getStatus());
        }

        example.setOrderByClause(promoteParam.getSort() + " " + promoteParam.getOrder());
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(example);
        return PromotePageData.ok(couponUsers);
    }

    @Override
    public Coupon updateCoupon(Coupon coupon) throws UpdateCouponException {
        if (coupon.getType() == CouponTypeEnum.CONVERSION_CODE.ordinal()){
            if (coupon.getCode() == null) {
                coupon.setCode(UUID.randomUUID().toString());
            }
        }else {
            coupon.setCode(null);
        }
        //如果优惠券时间类型发生了改变，就进行改变，如果没有改变，就不改变数据库中的数据
        Coupon selectCoupon = couponMapper.selectByPrimaryKey(coupon.getId());
        if (!coupon.getTimeType().equals(selectCoupon.getTimeType())){
            couponTimeType(coupon);
        }
        coupon.setUpdateTime(new Date(System.currentTimeMillis()));
        int code = couponMapper.updateByPrimaryKeySelective(coupon);
        if (code == 0) throw new UpdateCouponException();
        return coupon;
    }

    private void couponTimeType(Coupon coupon) {
        if (coupon.getTimeType() == CouponTimeTypeEnum.DAYS.ordinal()) {
            coupon.setStartTime(new Date(System.currentTimeMillis()));
            Date date = new Date();
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            if (coupon.getDays() == 0) {
                calendar.add(Calendar.DATE, 9999);
            } else {
                calendar.add(Calendar.DATE, coupon.getDays()); //把日期往后增加一天,整数  往后推,负数往前移动
            }
            date = calendar.getTime();//这个时间就是日期往后推days天的结果
            coupon.setEndTime(date);
        }
        if (coupon.getTimeType() == CouponTimeTypeEnum.TIMES.ordinal()) {
            long startTime = coupon.getStartTime().getTime();
            long endTime = coupon.getEndTime().getTime();
            long days = ((endTime - startTime) / (1000 * 3600 * 24));
            coupon.setDays((int) days);
        }
    }


}
